package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.xckj.cycling.R;
import com.xckj.cycling.databinding.ActivityForgetPasswordBinding;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IForgetPasswordView;
import com.xckj.cycling.iview.ISendCodeView;
import com.xckj.cycling.presenter.ForgetPasswordPresenter;
import com.xckj.cycling.presenter.SendCodePresenter;
import com.xckj.cycling.utils.AndroidUtil;
import com.xckj.cycling.utils.CheckFormatUtil;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 忘记密码
 * @Date: 2023/3/1 23:29
 */
public class ForgetPasswordActivity extends BaseActivity<ActivityForgetPasswordBinding> implements IForgetPasswordView, ISendCodeView {
    private ForgetPasswordPresenter forgetPasswordPresenter;
    private SendCodePresenter sendCodePresenter;

    @Override
    protected int loadLayout() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        dataBinding.viewTitle.tvTitle.setText(getString(R.string.forget_password));
    }

    @Override
    protected void initData() {
        forgetPasswordPresenter = new ForgetPasswordPresenter(this);
        sendCodePresenter = new SendCodePresenter(this);
    }

    @Override
    protected void setListener() {
        dataBinding.cvRegisterCountdown.setOnClickListener(this);
        dataBinding.btnFinish.setOnClickListener(this);
        dataBinding.etConfirmPassword.addTextChangedListener(textWatcher);
        dataBinding.etPassword.addTextChangedListener(textWatcher);
        dataBinding.etMailbox.addTextChangedListener(textWatcher);
        dataBinding.etVerificationCode.addTextChangedListener(textWatcher);

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        String mailbox = dataBinding.etMailbox.getText().toString();
        String code = dataBinding.etVerificationCode.getText().toString();
        String password = Objects.requireNonNull(dataBinding.etPassword.getText()).toString();
        String confirmPassword = Objects.requireNonNull(dataBinding.etConfirmPassword.getText()).toString();
        if (AndroidUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.cv_register_countdown:
                sendCode(mailbox);
                break;
            case R.id.btn_finish:
                finish(mailbox, code, password, confirmPassword);
                break;
            default:
                break;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            checkButton();
        }
    };

    private void checkButton() {
        boolean b = isNotEmpty(dataBinding.etConfirmPassword) && isNotEmpty(dataBinding.etPassword) && isNotEmpty(dataBinding.etMailbox)
                && isNotEmpty(dataBinding.etVerificationCode);
        dataBinding.btnFinish.setEnabled(b);
    }

    private void sendCode(String mailbox) {
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        sendCodePresenter.sendCode("resetpwd", mailbox);
    }

    private void finish(String mailbox, String code, String password, String confirmPassword) {
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showMessage(getString(R.string.code_null_hint));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showMessage(getString(R.string.password_hint));
            return;
        }
        if (password.length() <= Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (TextUtils.isEmpty(confirmPassword)) {
            showMessage(getString(R.string.password_null_hint));
            return;
        }
        if (confirmPassword.length() <= Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (!password.equals(confirmPassword)) {
            showMessage(getString(R.string.password_compare));
            return;
        }
        goToFinish(mailbox, code, password);
    }

    private void goToFinish(String mailbox, String code, String password) {
        forgetPasswordPresenter.forgetPassword(mailbox, code, password);
    }

    @Override
    public void success(String message) {
        showMessage(message);
        finish();
    }

    @Override
    public void failure(String message) {
        showMessage(message);
    }

    @Override
    public void sendCodeSuccess(String message) {
        showMessage(getString(R.string.send_code_hint));
        dataBinding.cvRegisterCountdown.start();
    }

    @Override
    public void sendCodeFailure(String message) {
        showMessage(message);
    }
}
